﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using mjRegister.BAL;
using System.IO;
using System.Text.RegularExpressions;

namespace mjRegister
{
    public partial class sohuemail : Form
    {
        int curnum = 0;

        int start = 0;
        int end = 0;
        int length = 0;

        SohuVerifyCode sohuVerifyCode = new SohuVerifyCode();

        public sohuemail()
        {
            InitializeComponent();
        }

        void changeemail()
        {
            if (!this.checkBox1.Checked)
            {
                try
                {
                    start = int.Parse(this.txtStart.Text.Trim());
                    end = int.Parse(this.txtEnd.Text.Trim());
                    length = int.Parse(this.txtLength.Text.Trim());

                    if (curnum == 0)
                        curnum = start;

                    this.textBox2.Text = curnum.ToString();

                    this.label1.Text = string.Format("总计：{0}", end - start + 1);

                    this.lemail.Text = string.Format("{0}@{1}", 
                        this.txtemail.Text.Replace("{*}", curnum.ToString().PadLeft(length, '0')),
                        this.comboBox1.SelectedIndex == 0 ? "sohu.com" : "sogou.com");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("[范围]、[长度]数据类型：数值。");
                }
            }
            else
            {
                this.lemail.Text = string.Format("{0}@{1}",
                    ((char)new Random().Next(97, 122)).ToString() + Guid.NewGuid().ToString().Substring(0, new Random().Next(5, 15)).Replace("-", ""),
                    this.comboBox1.SelectedIndex == 0 ? "sohu.com" : "sogou.com");
            }

        }

        private void txtStart_TextChanged(object sender, EventArgs e)
        {
            changeemail();
        }

        private void txtEnd_TextChanged(object sender, EventArgs e)
        {
            changeemail();
        }

        private void txtLength_TextChanged(object sender, EventArgs e)
        {
            changeemail();
        }

        private void txtemail_TextChanged(object sender, EventArgs e)
        {
            changeemail();
        }

        private void sohuemail_Load(object sender, EventArgs e)
        {
            this.comboBox1.SelectedIndex = 0;
            GetExt();
            changeemail();
        }

        public void GetExt()
        {
            this.piccode.Image = sohuVerifyCode.GetVerifyCodeImage();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            GetExt();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //string postUrl = this.comboBox1.SelectedIndex == 0 ?
            //    "http://passport.sohu.com/web/Passportregister.action?appid=1000&autologin=1&ru=http://login.mail.sohu.com/reg/signup_success.jsp" :
            //    "http://passport.sohu.com/web/Passportregister.action?appid=1014&autologin=1&ru=http://mail.sogou.com/2gmail/login.jsp";


            string postUrl = "http://passport.sohu.com/web/Passportregister.action";

            string postData = string.Format("shortname={0}&validate={1}&user.domain={2}&user.password={3}&password2={3}&user.question={4}&user.answer={5}&user.personalid=&user.mobile=&user.email=&uuidCode={6}",
                                                this.lemail.Text.Split('@')[0],
                                                txtcode.Text,
                                                this.comboBox1.SelectedIndex == 0 ? "sohu.com" : "sogou.com",
                                                this.textBox1.Text,
                                                Common.UrlEncode("我就读的幼儿园名称"),
                                                Common.UrlEncode("wj2929"),
                                                "LCENJXLALJSGAEPAXLCENJGLSCJAXLALJSGHEUNUHNY");


            HttpProc hp = new HttpProc(postUrl, postData, sohuVerifyCode.cc);
            hp.SetEncoding(Encoding.Default);
            string ResStr1 = hp.Proc();
            if (ResStr1.IndexOf("恭喜您已经注册成功") > 0)
            {
                using (StreamWriter sw = new StreamWriter(Path.Combine(Application.StartupPath, "tom_email_account.txt"), true, Encoding.Default))
                {
                    sw.WriteLine(string.Format("{0}@tom.com {1}", this.lemail.Text, this.textBox1.Text));
                }
                using (StreamWriter sw = new StreamWriter(Path.Combine(Application.StartupPath, "tom_emails.txt"), true, Encoding.Default))
                {
                    sw.WriteLine(string.Format("{0}@tom.com", this.lemail.Text));
                }
                curnum++;
                MessageBox.Show("恭喜您已经注册成功！");
                this.txtcode.Focus();
                if (curnum > end)
                {
                    MessageBox.Show("批量注册完毕！");
                    return;
                }
                GetExt();
                changeemail();
            }
            else
            {
                string re = "name=\\\"info\\\".*?value=\\\"(?<info>.*?)\\\"";
                if (Regex.IsMatch(ResStr1, re))
                {
                    string message = Regex.Match(ResStr1, re).Result("${info}");
                    if (message == "验证码验证失败")
                        GetExt();
                    else if (message == "这个名字已经有人使用了")
                        changeemail();
                    MessageBox.Show(message);
                }
                else
                {
                    MessageBox.Show("未知错误！");
                    GetExt();
                    changeemail();
                }
            }
            this.txtcode.Text = string.Empty;


        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                curnum = int.Parse(this.textBox2.Text.Trim());
            }
            catch (Exception ex)
            {
                MessageBox.Show("[当前计数]数据类型：数值。");
            }

            changeemail();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            this.groupBox1.Enabled = !this.checkBox1.Checked;
            if (this.checkBox1.Checked)
            {
                this.linkLabel2.Enabled = true;
                changeemail();
            }
            else
                this.linkLabel2.Enabled = false;
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            changeemail();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            changeemail();
        }
    }
}
